import Vue from 'vue'
import VueRouter from 'vue-router'
// import login from '../components/login.vue'
const login = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/login.vue')
// import home from '../components/home.vue'
const home = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/home.vue')
// import welcom from '../components/welcom.vue'
const welcom = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/welcom.vue')

// import users from '../components/users.vue'
const users = () => import(/* webpackChunkName:"users_roles_rights" */ '../components/users.vue')
// import roles from '../components/power/roles.vue'
const roles = () => import(/* webpackChunkName:"users_roles_rights" */ '../components/power/roles.vue')
// import rights from '../components/power/rights.vue'
const rights = () => import(/* webpackChunkName:"users_roles_rights" */ '../components/power/rights.vue')

// import cate from '../components/goods/cate.vue'
const cate = () => import(/* webpackChunkName:"cate_params" */ '../components/goods/cate.vue')
// import params from '../components/goods/params.vue'
const params = () => import(/* webpackChunkName:"cate_params" */ '../components/goods/params.vue')

// import goods from '../components/goods/list.vue'
const goods = () => import(/* webpackChunkName:"goods" */ '../components/goods/list.vue')
// import Add from '../components/goods/add.vue'
const Add = () => import(/* webpackChunkName:"goods" */ '../components/goods/add.vue')

// import reports from '../components/report/reports.vue'
const reports = () => import(/* webpackChunkName:"repo_orders" */ '../components/report/reports.vue')
// import orders from '../components/order/orders.vue'
const orders = () => import(/* webpackChunkName:"repo_orders" */ '../components/order/orders.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    children: [
      {
        path: '/',
        redirect: '/welcom'
      },
      {
        path: '/welcom',
        component: welcom
      },
      {
        path: '/users',
        component: users
      },
      {
        path: '/roles',
        component: roles
      }, {
        path: '/goods',
        component: goods
      }, {
        path: '/params',
        component: params
      }, {
        path: '/categories',
        component: cate
      }, {
        path: '/orders',
        component: orders
      }, {
        path: '/reports',
        component: reports
      }, {
        path: '/rights',
        component: rights
      }, {
        path: '/goods/add',
        component: Add
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 表示将要访问的路径
  // from 表示从哪个路径跳转而来
  // next 是一个函数，next()表示放行,next('/login')表示强制跳转到/login
  // 登录页无条件放行
  if (to.path === '/login') {
    console.log(1)
    next()
  } else {
    // 其他页面需要查看是否有token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) {
      console.log('unlogon')
      if (from.path === to.path) {
        console.log('from' + from)
        console.log(to)
        return next()
      }
      console.log(from)
      console.log(to)
      console.log(2)
      return next('/login')
    } else {
      console.log(3)
      next()
    }
  }
})
export default router
